package com.hyh.prize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyh.prize.entity.TabSaleplanGiftcode;
import com.hyh.prize.entity.TabSaleplanGiftstock;
import com.hyh.prize.mapper.GgkStockMapper;
import com.hyh.prize.service.DictSerive;
import com.hyh.prize.service.GgkStockService;
import com.hyh.prize.vo.GgkConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GgkStockServiceImpl extends ServiceImpl<GgkStockMapper, TabSaleplanGiftstock> implements GgkStockService {

    @Resource
    private DictSerive dictSerive;
    @Resource
    private GgkStockService ggkStockService;

    @Resource
    private  GiftServiceImpl giftService;
    @Override
    //根据营业厅id查询礼品库存信息
    public List<TabSaleplanGiftstock> getStockList(String id) {
        QueryWrapper<TabSaleplanGiftstock> queryWrapper=new QueryWrapper();
        queryWrapper.eq("ACPTSITEID",id);

        List<TabSaleplanGiftstock> tabSaleplanGiftstocks = list(queryWrapper);
        return tabSaleplanGiftstocks;
    }


    //添加礼品配置，对库存添加记录
    public boolean insetrGgkConfig(GgkConfig ggkConfig){
        System.out.println("添加礼品信息："+ggkConfig);
        TabSaleplanGiftstock tabSaleplanGiftstock=new TabSaleplanGiftstock();

        //通过营业厅id和礼品id获得库存信息
        TabSaleplanGiftstock ggkStock = this.getGgkStock(ggkConfig.getAcptsiteid(), ggkConfig.getGiftcode());
        if(ggkStock!=null){
            //如果有数据，执行修改操作
            ggkStock.setTotalnum(ggkConfig.getXnumber());
            ggkStock.setStocknum(ggkStock.getStocknum()+ggkConfig.getXnumber());
            ggkStock.setLastIn(ggkConfig.getXnumber());

            return this.update(ggkStock);
        }else{//如果不存在就添加记录

            //配置库存信息
            TabSaleplanGiftstock giftstock=new TabSaleplanGiftstock();
            //1.通过营业厅id获取当前地区名称
            String dName = dictSerive.getDCode(ggkConfig.getAcptsiteid());
            giftstock.setAreaid(dName);
            //配置库存数量
            giftstock.setTotalnum(ggkConfig.getXnumber());
            giftstock.setStocknum(ggkConfig.getXnumber());
            giftstock.setLastIn(ggkConfig.getXnumber());
            //其他信息
            giftstock.setAcptsiteid(ggkConfig.getAcptsiteid());
            giftstock.setGiftcode(ggkConfig.getGiftcode());
            //添加
            boolean b = this.insertGigstock(giftstock);
            return b;
        }
    }



    /**
     * 通过营业厅id和礼品id获得库存信息
     * @param code 营业厅id
     * @param lpId 礼品id
     * @return
     */
    public TabSaleplanGiftstock getGgkStock(String code,String lpId){
        QueryWrapper<TabSaleplanGiftstock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ACPTSITEID",code);
        queryWrapper.eq("GIFTCODE",lpId);
        TabSaleplanGiftstock tabSaleplanGiftstock = this.getOne(queryWrapper);
        return tabSaleplanGiftstock;
    }

    /**
     * 更新库存
     * @param tabSaleplanGiftstock
     * @return
     */
    public boolean update(TabSaleplanGiftstock tabSaleplanGiftstock){
        boolean b = this.updateById(tabSaleplanGiftstock);
        return b;
    }
    /**
     * 新增库存记录
     * @param tabSaleplanGiftstock
     * @return
     */
    public boolean insertGigstock(TabSaleplanGiftstock tabSaleplanGiftstock){
        boolean b = this.save(tabSaleplanGiftstock);
        return b;
    }

    /**
     * 更新多条记录
     * @param list
     * @return
     */
    public boolean updateAll(List<GgkConfig> list){

        for (GgkConfig ggkConfig : list) {
            if(ggkConfig.getXnumber()==null || ggkConfig.getXnumber().equals("0")){
                continue;
            }
            boolean b = this.insetrGgkConfig(ggkConfig);
            if(!b){
                System.out.println("添加库存时出错！");
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean receive(String lpid, Integer num,String yid) {
        //获得当前库存的详情信息
        TabSaleplanGiftstock ggkStock = this.getGgkStock(yid, lpid);
        if(ggkStock!=null){
            if(ggkStock.getStocknum()>num){
                //领取礼品
                ggkStock.setStocknum(ggkStock.getStocknum()-num);
                ggkStock.setObtainnum(ggkStock.getObtainnum()+num);
                update(ggkStock);
                return true;
            }
        }
        return false;
    }



    /**
     * 根据等级id获得所有礼品
     * @param id
     * @return
     */
    public List lpByLevelId(String id,String typeCode,String yytId){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!StringUtils.isEmpty(typeCode)){
            queryWrapper.eq("GIFTTYPE",typeCode);
        }
        queryWrapper.eq("ACPTSITEID",yytId);
        queryWrapper.eq("PRIZELEVEL",id);
        List<TabSaleplanGiftcode> list = giftService.list(queryWrapper);
        list.stream().forEach(item ->{
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("GIFTCODE",item.getGiftcode());
            queryWrapper1.eq("ACPTSITEID",item.getAcptsiteid());
            TabSaleplanGiftstock ggkStockServiceOne = ggkStockService.getOne(queryWrapper1);
            if(ggkStockServiceOne!=null){
                item.setNumber(ggkStockServiceOne.getStocknum()+"");
                item.setYnumber(ggkStockServiceOne.getObtainnum()+"");
                item.setTypeName(dictSerive.getLpType(item.getGifttype()));
            }

        });
        return list;
    }
}
